package com.qianfeng.zhangwei.volley.request;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.AuthFailureError;
import com.android.volley.Cache;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @Package com.qianfeng.zw.day36.request
 * @作 用:自定义FastRequest
 * @创 建 人: zhangwei
 * @日 期: 15/4/27 15:16
 * @修 改 人:
 * @日 期:
 */
public class FastJsonRequest<T> extends Request<T> {
    private Response.Listener<T> listener;
    private Class<?> cls;
    private T t;

    private Map<String, String> params;

    private Map<String, String> headers;


    public FastJsonRequest(int method, Class<?> cls, String url, Map<String, String> params, Map<String, String> headers, Response.Listener listener, Response.ErrorListener errorListener) {
        this(method, cls, url, params, listener, errorListener);
        this.headers = headers;
    }


    /**
     * post请求 需要
     *
     * @param method
     * @param cls
     * @param url
     * @param listener
     * @param errorListener
     */


    public FastJsonRequest(int method, Class<?> cls, String url, Map<String, String> params, Response.Listener listener, Response.ErrorListener errorListener) {
        this(method, url, listener, errorListener);
        this.cls = cls;
        this.params = params;
    }

    /**
     * @param method
     * @param cls
     * @param url
     * @param listener
     * @param errorListener
     */
    public FastJsonRequest(int method, Class<?> cls, String url, Response.Listener listener, Response.ErrorListener errorListener) {
        this(method, url, listener, errorListener);
        this.cls = cls;
    }

    /**
     * @param cls
     * @param url
     * @param listener
     * @param errorListener
     */
    public FastJsonRequest(Class<?> cls, String url, Response.Listener listener, Response.ErrorListener errorListener) {
        this(Method.GET, url, listener, errorListener);
        this.cls = cls;
    }

    /**
     * @param method
     * @param url
     * @param listener
     * @param errorListener
     */
    private FastJsonRequest(int method, String url, Response.Listener listener, Response.ErrorListener errorListener) {
        this(method, url, errorListener);
        this.listener = listener;
    }

    public FastJsonRequest(int method, String url, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        byte[] bytes = response.data; //(相当于返回的数据jsonStr)
        try {
            String jsonStr = new String(bytes, HttpHeaderParser.parseCharset(response.headers));
            t = (T) JSONObject.parseObject(jsonStr, cls);
            Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
            return Response.success(t, entry);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e));
        }
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return headers != null && headers.size() > 0 ? headers :
                super.getHeaders();
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params != null && params.size() > 0 ? params : super.getParams();
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);

    }


}
